<template>
  <div class="app-container">
    <div class="block">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-input
            v-model="listQuery.mobile"
            size="mini"
            placeholder="手机号"
          ></el-input>
        </el-col>
        <el-col :span="4">
          <el-input
            v-model="listQuery.nickName"
            size="mini"
            placeholder="昵称"
          ></el-input>
        </el-col>
        <el-col :span="8">
          <el-date-picker
            v-model="regDate"
            size="mini"
            type="daterange"
            range-separator="至"
            value-format="yyyy-MM-dd"
            start-placeholder="注册开始日期"
            end-placeholder="注册截至日期"
          >
          </el-date-picker>
        </el-col>
        <el-col :span="8">
          <el-date-picker
            v-model="lastLoginTime"
            size="mini"
            type="daterange"
            range-separator="至"
            value-format="yyyy-MM-dd"
            start-placeholder="最近登录开始日期"
            end-placeholder="最近登录截至日期"
          >
          </el-date-picker>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="6">
          <el-button
            type="success"
            size="mini"
            icon="el-icon-search"
            @click.native="search"
            >搜索</el-button
          >
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-refresh"
            @click.native="reset"
            >重置</el-button
          >
        </el-col>
      </el-row>
    </div>
    <el-table
      :data="list"
      v-loading="listLoading"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      @current-change="handleCurrentChange"
    >
      <el-table-column label="ID">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="头像">
        <template>
          <img class="headImage" src="../../../1.png" alt="" />
        </template>
      </el-table-column>
      <el-table-column label="昵称">
        <template slot-scope="scope">
          {{ scope.row.nickName }}
        </template>
      </el-table-column>
      <el-table-column label="手机号">
        <template slot-scope="scope">
          <router-link :to="{ path: './detail?id=' + scope.row.id }">
            {{ scope.row.mobile }}
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="性别">
        <template slot-scope="scope">
          {{ scope.row.gender }}
        </template>
      </el-table-column>
      <el-table-column label="注册时间">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column label="最后登陆时间">
        <template slot-scope="scope">
          {{ scope.row.lastLoginTime }}
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[10, 20, 50, 100, 500]"
      :page-size="listQuery.limit"
      :total="total"
      :current-page.sync="listQuery.page"
      @size-change="changeSize"
      @current-change="fetchPage"
      @prev-click="fetchPrev"
      @next-click="fetchNext"
    >
    </el-pagination>
  </div>
</template>
<script>
import { getList, remove, save } from "@/api/shopUser";
export default {
  name: "CommodityManagement",
  data() {
    return {
      formVisible: false,
      formTitle: "添加用户",
      isAdd: true,
      form: {
        mobile: "",
        salt: "",
        password: "",
        nickName: "",
        avatar: "",
        id: ""
      },
      regDate: undefined,
      lastLoginTime: undefined,
      listQuery: {
        page: 1,
        limit: 20,
        mobile: undefined,
        nickName: undefined,
        startRegDate: undefined,
        endRegDate: undefined,
        startLastLoginTime: undefined,
        endLastLoginTime: undefined
      },
      total: 0,
      list: null,
      listLoading: true,
      selRow: {}
    };
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "gray",
        deleted: "danger"
      };
      return statusMap[status];
    }
  },
  created() {
    let _self = this;
    _self.init();
  },
  methods: {
    init() {
      let _self = this;
      _self.fetchData();
    },
    fetchData() {
      let _self = this;
      _self.listLoading = true;

      if (_self.regDate) {
        _self.listQuery["startRegDate"] = _self.regDate[0];
        _self.listQuery["endRegDate"] = _self.regDate[1];
      }
      if (_self.lastLoginTime) {
        _self.listQuery["startLastLoginTime"] = _self.lastLoginTime[0];
        _self.listQuery["endLastLoginTime"] = _self.lastLoginTime[1];
      }

      getList(_self.listQuery).then(response => {
        let _self = this;
        _self.list = response.data.records;
        for (const index in _self.list) {
          let item = _self.list[index];
          if (item.wechatHeadImgUrl) {
            item.img = item.wechatHeadImgUrl;
          } else {
            item.img =
              "http://localhost:9528/dev-api/file/getImgStream?idFile=" +
              item.avatar;
          }
          console.log("img", item.img);
        }
        _self.listLoading = false;
        _self.total = response.data.total;
      });
    },
    search() {
      let _self = this;
      _self.listQuery.page = 1;
      _self.listQuery.mobile = _self.listQuery.mobile.trim();
      _self.listQuery.nickName = _self.listQuery.nickName.trim();
      if (_self.listQuery.mobile == "" || _self.listQuery.nickName == "") {
        _self.$message({
          message: "手机号和用户名都需填写",
          type: "warning"
        });
      } else {
        _self.fetchData();
      }
    },
    reset() {
      let _self = this;
      _self.listQuery.mobile = "";
      _self.listQuery.nickName = "";
      _self.listQuery.startRegDate = "";
      _self.listQuery.endRegDate = "";
      _self.listQuery.startLastLoginTime = "";
      _self.listQuery.endLastLoginTime = "";
      _self.lastLoginTime = "";
      _self.regDate = "";
      _self.listQuery.page = 1;
      _self.fetchData();
    },
    handleFilter() {
      let _self = this;
      _self.listQuery.page = 1;
      _self.getList();
    },
    handleClose() {},
    fetchNext() {
      let _self = this;
      _self.listQuery.page = _self.listQuery.page + 1;
      _self.fetchData();
    },
    fetchPrev() {
      let _self = this;
      _self.listQuery.page = _self.listQuery.page - 1;
      _self.fetchData();
    },
    fetchPage(page) {
      let _self = this;
      _self.listQuery.page = page;
      _self.fetchData();
    },
    changeSize(limit) {
      let _self = this;
      _self.listQuery.limit = limit;
      _self.fetchData();
    },
    handleCurrentChange(currentRow, oldCurrentRow) {
      let _self = this;
      _self.selRow = currentRow;
    },
    checkSel() {
      let _self = this;
      if (_self.selRow && _self.selRow.id) {
        return true;
      }
      _self.$message({
        message: "请选择一项",
        type: "warning"
      });
      return false;
    }
  }
};
</script>
<style scoped lang="">
.headImage {
  width: 50px;
  height: 50px;
}
.block {
  padding: 10px 0px;
}
.el-form-item {
  margin-bottom: 2px;
}
.flash-table-expand {
  font-size: 0;
}

.flash-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.user {
  padding: 10px;
}
.user > strong {
  color: #777;
  font-size: 15px;
  font-weight: 400;
}

.user > small {
  color: #9e9e9e;
}

.user-content {
  box-shadow: 2px 2px 4px #edecec;
  min-height: 500px;
}

.user-content > .profile {
  background: #edecec;
  padding: 10px;
  height: 500px;
  width: 220px;
}
.user-content > .profile > img {
  width: 100%;
}

a {
  color: lightblue;
}

.el-select {
  width: 100%;
}
.el-row {
  margin-bottom: 20px;
}
.el-row :last-child {
  margin-bottom: 0;
}
</style>
